package com.objecteffects.ilmp.web.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.inject.Singleton;

import org.apache.commons.lang3.exception.ExceptionUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sourceforge.stripes.util.Base64;

/**
 * @author Rusty Wright
 */
@Singleton
public class IlmpHasher {
    private final transient Logger log = LoggerFactory
        .getLogger(IlmpHasher.class);

    private static final String DIGEST = "SHA-1";

    public String hash(final String string) {
        MessageDigest md;

        try {
            md = MessageDigest.getInstance(IlmpHasher.DIGEST);
        }
        catch (final NoSuchAlgorithmException ex) {
            this.log.error(ExceptionUtils.getStackTrace(ex));

            throw new IllegalArgumentException(ex);
        }

        final byte[] bytes = md.digest(string.getBytes());

        return Base64.encodeBytes(bytes);
    }

}
